/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package codebar;

import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
 *
 * @author yole
 */
public class Utils {
    
    public enum Type {YEAR,NORDEM,FORUM,VARA,ANEXO};
    
    public static String extract(Type type, String forum, String code) {
        if ( forum.compareTo("11") != 0 ) {
            switch(type) {
                case YEAR:
                    return code.substring(4, 8);
                case NORDEM:
                    return code.substring(8,14);
                case FORUM:
                    return code.substring(0,2);
                case VARA:
                    return code.substring(2,4);
                case ANEXO:
                    return code.substring(14,20);
                default:
                    return "";
            }
        } else {
            switch(type) {
                case YEAR:
                    return code.substring(5, 9);
                case NORDEM:
                    return code.substring(9,15);
                case FORUM:
                    return code.substring(0,2);
                case VARA:
                    return code.substring(2,5);
                case ANEXO:
                    return code.substring(15,21);
                default:
                    return "";
            }
        }
    }
    
    public static boolean isNumber(String s) {
        
        boolean cond = true;
        char [] cbuff = s.toCharArray();
        
        for ( int i = 0; i < s.length(); i++ ) {
            if (cbuff[i] < 48 || cbuff[i] > 57) {
                cond = false;
                break;
            }
        }
        
        return cond;
    }
    
    public static void exportToFile(String s,File file) throws IOException {
        
        BufferedWriter fwriter = new BufferedWriter(new FileWriter(file));
        fwriter.write(s);
        fwriter.close();
        
    }
    
    public static void importFromFile(File file, CodeList codeList) throws IOException {
        BufferedReader freader = new BufferedReader( new FileReader(file));

        String test;
        String year = new String("-1");
        
        
        while ((test = freader.readLine()) != null) {
            if (test.compareTo("") != 0) {
                if (test.charAt(0) == 'A')
                    year = test.substring(2);
                if (test.charAt(0) == 'P') {
                    try {
                        codeList.add(year, test.substring(2));
                    } catch (DuplicatedDataException ex) {
                        System.out.println("Dados duplicados");
                    }
                }
            }
        }
    }
    
     public static String getLegibleContent(CodeList codeList) {
        String s;
        
        s = "Total de processos: " + codeList.total()+ "\n\n";
        for (int i = 0; i < codeList.size(); i++) {
            s +="\nAno: " + codeList.getYear(i)+"\nNúmero de processos: " 
                    + codeList.get(i).size() + "\n\nProcessos:\n";
            for (int j = 0; j < codeList.get(i).size(); j++) {
                s += codeList.get(i).getNOrdem(j) + "\n";
            }
        }     
        
        return s;
    }
     
     public static void export(String data) {
        JFileChooser fileChooser = new JFileChooser(System.getProperty("user.dir"));  
        fileChooser.setFileFilter( new ExtensionFilter("Arquivo Texto",new String[]{"txt"}));
        int returnVal = fileChooser.showSaveDialog(null);
        if (returnVal == 0) {
            try {
                int op = 0;
                String sub = fileChooser.getName(fileChooser.getSelectedFile());
                if (fileChooser.getFileFilter().getDescription().compareTo("Arquivo Texto") == 0) {
                    String s = fileChooser.getName(fileChooser.getSelectedFile());
                    if (s.contains( new String(".") )) {
                        sub = s.substring(0, s.indexOf('.'));
                        sub += ".txt";
                    } else
                        sub += ".txt";
                }
                File f = new File(sub);
                if (f.exists()) {
                    op = JOptionPane.showConfirmDialog(fileChooser, "Arquivo já existente. Deseja sobrescreve-lo?",
                            null,JOptionPane.YES_NO_OPTION);
                }
                if (op == 0)
                    Utils.exportToFile(data, f);
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(null, "Não foi possível exportar os dados para o arquivo.");
            }
        }
    }
    
     public static void importFile(CodeList codeList) {
        JFileChooser fileChooser = new JFileChooser(System.getProperty("user.dir"));  
        fileChooser.setFileFilter( new ExtensionFilter("Arquivo Texto",new String[]{"txt"}));
        int returnVal = fileChooser.showSaveDialog(null);
        if (returnVal == 0) {
            try {
                int op = 0;
                String sub = fileChooser.getName(fileChooser.getSelectedFile());
                if (fileChooser.getFileFilter().getDescription().compareTo("Arquivo Texto") == 0) {
                    String s = fileChooser.getName(fileChooser.getSelectedFile());
                    if (s.contains( new String(".") )) {
                        sub = s.substring(0, s.indexOf('.'));
                        sub += ".txt";
                    } else
                        sub += ".txt";
                }
                File f = new File(sub);
                Utils.importFromFile(f, codeList);
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(null, "Não foi possível exportar os dados para o arquivo.");
            }
        }
    }
     
     public static String forgeProc(int proc) {
         String s = String.valueOf(proc);
         
         int n = 6 - s.length();
         
         String aux = new String("");
         
         for ( int i = 0; i < n; i++ )
             aux += "0";
         
         aux += s;
         
         return aux;
         
     }
    
}
